package com.sright.bcard.user.biz.service;

import com.sright.bcard.user.biz.manager.AuthManager;
import com.sright.bcard.common.service.share.utils.JWTUitls;
import com.sright.bcard.common.service.share.dto.user.RegisterDTO;
import com.sright.bcard.user.share.service.AuthService;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.text.ParseException;

@Component
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthManager authManager;

    @Override
    public SrightResponse register(RegisterDTO dto) throws ParseException {
        return SrightResponse.defaultSuccess(authManager.register(dto));
    }

    @Override
    public SrightResponse login(String userName, String password) {
        return SrightResponse.defaultSuccess(authManager.login(userName, password));
    }

    @Override
    public Long queryUserIdByToken(String token) {
        if (StringUtils.isEmpty(token)) {
            return 0L;
        }
        return JWTUitls.validateToken(token);
    }
}
